<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>改变数组元素</title>
</head>
<body>
	<h1>改变数组元素</h1><hr>
	<script type="text/javascript">
		//创建个数组,Array()首字母大写.未赋值长度为0
		var myArray = new Array();
		document.write("赋值前内容为: ");
		//for循环,输出数组内容
		for(var i=0; i<=myArray.length; i++) {
			document.write(myArray[i] + " ");
		}
		//同样可以使用for in循环输出
//		for(var i in myArray) {
//			document.write(myArray[i] + " ");
//		}
		document.write(myArray.length);
		document.write("<br>");
		
		//第一次赋值,把值等于下标
		for(var i=0; i < 10; i++) {
			myArray[i] = i;
		}
		document.write("第一次赋值后内容为: ");
		for(var i=0; i<myArray.length; i++) {
			document.write(myArray[i] + " ");
		}
		document.write("<br>");
		
		//第二次赋值,每隔1个元素加2
		for(var i=0; i < 10; i=i+2) {
			myArray[i] = i+2;
		}
		document.write("第二次赋值后内容为: ");
		for(var i=0; i<myArray.length; i++) {
			document.write(myArray[i] + " ");
		}
		document.write("<br>");
		
		//第三次赋值,增加第12元素为this,中间多出来的为undefined
		myArray[12] = "this";
		document.write("第三次赋值后内容为: ");
		for(var i=0; i<myArray.length; i++) {
			document.write(myArray[i] + " ");
		}
		document.write("<br>");
		
		//第四次赋值,删除第3和第13个元素,值变为undefined
		delete myArray[2];
		delete myArray[12];
		document.write("第四次赋值后内容为: ");
		for(var i=0; i<myArray.length; i++) {
			document.write(myArray[i] + " ");
		}
		document.write("<br>");
		
		//第五次赋值,重置长度为6,相当于截断后面的
		myArray.length = 6;
		document.write("第五次赋值后内容为: ");
		for(var i=0; i<myArray.length; i++) {
			document.write(myArray[i] + " ");
		}
	</script>

</body>
</html>